/*
 * @Author: mikey.zhaoyd
 * @Date: 2019-02-16 10:30:37
 * @Last Modified by: mikey.zhaoyd
 * @Last Modified time: 2019-02-16 13:52:49
 */
#include <stdio.h>
#include <string.h>
//#include "hal_key.h"
#include "common.h"
#include "product.h"
#include "gpio.h"

#include "driverPm25.h"
#include "driverEsp12F.h"


uint32_t timerMsCount = 0;
uint32_t timer100MsCount = 0;                 // 19/02/16 12:03:02: [zyd]:100ms次数
uint8_t  aRxBuffer1, aRxBuffer2, aRxBuffer3;  // 19/01/23 04:37:05: [zyd]:串口收到的一个字节

extern UART_HandleTypeDef huart1;
extern UART_HandleTypeDef huart2;
extern UART_HandleTypeDef huart3;

rb_t           rb1, rb2, rb3;                                               ///< Ring buffer structure variable
static uint8_t rbBuf1[RB_MAX_LEN], rbBuf2[RB_MAX_LEN], rbBuf3[RB_MAX_LEN];  ///< Ring buffer data cache buffer

void userHandle(void)
{
//   int8_t ret=0;
//   
//   ret += HandleSensorPm25Rx(&rb1);
//   HandleEsp12F();
   static uint16_t cnt=0;
   
   EMLedState LedState;
   
   if(cnt%2==0)
   {
      LedState=LEDON;
   }
   else
   {
      LedState=LEDOFF;
   }
   cnt++;
   LED0OnOff(LedState);
}

/**
* Data point initialization function

* In the function to complete the initial user-related data
* @param none
* @return none
* @note The developer can add a data point state initialization value within
this function
*/
void userInit(void)
{
    // memset((uint8_t*)&currentDataPoint, 0, sizeof(dataPoint_t));
    ringBufferInit();  // 19/01/23 04:29:16: [zyd]:初始化环形缓冲区

    HAL_UART_Receive_IT(&huart1, (uint8_t*)&aRxBuffer1, 1);  //开启下一次接收中断
    HAL_UART_Receive_IT(&huart2, (uint8_t*)&aRxBuffer2, 1);  //开启下一次接收中断
    HAL_UART_Receive_IT(&huart3, (uint8_t*)&aRxBuffer3, 1);  //开启下一次接收中断
}

#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
   set to 'Yes') calls __io_putchar() */
#    define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#    define PUTCHAR_PROTOTYPE int fputc(int ch, FILE* f)
#endif /* __GNUC__ */
/**
 * @brief  Retargets the C library printf function to the USART.
 * @param  None
 * @retval None
 */
PUTCHAR_PROTOTYPE
{
    /* Place your implementation of fputc here */
    /* e.g. write a character to the USART1 and Loop until the end of
     * transmission */
    HAL_UART_Transmit(&huart2, (uint8_t*)&ch, 1, 0xFFFF);

    return ch;
}

/**
 * @brief  Period elapsed callback in non blocking mode
 * @param  htim : TIM handle
 * @retval None
 */
// void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
//{
//	if(htim==&htim2)
//	{
//			keyHandle((keysTypedef_t *)&keys);
//			gizTimerMs();
//	}
//}

/**
* @brief Timer TIM3 init function

* @param none
* @return none
*/
// void timerInit(void)
//{
//	HAL_TIM_Base_Start_IT(&htim2);
//}

/**
 * @brief  This function handles USART IDLE interrupt.
 * 串口接收中断 重新编写
 * 原来的\Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_uart.c有一个弱定义。
 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef* UartHandle)
{
    if (UartHandle->Instance == USART1)
    {
        gizPutData(&rb1, (uint8_t*)&aRxBuffer1,
                   1);  // 19/01/24 09:07:40: [zyd]:将数据写入环形缓冲区

        HAL_UART_Receive_IT(UartHandle, (uint8_t*)&aRxBuffer1,
                            1);  //开启下一次接收中断
    }
    if (UartHandle->Instance == USART2)
    {
        gizPutData(&rb2, (uint8_t*)&aRxBuffer2,
                   1);  // 19/01/24 09:07:40: [zyd]:将数据写入环形缓冲区

        HAL_UART_Receive_IT(UartHandle, (uint8_t*)&aRxBuffer2,
                            1);  //开启下一次接收中断
    }
    if (UartHandle->Instance == USART3)
    {
        gizPutData(&rb3, (uint8_t*)&aRxBuffer3,
                   1);  // 19/01/24 09:07:40: [zyd]:将数据写入环形缓冲区

        HAL_UART_Receive_IT(UartHandle, (uint8_t*)&aRxBuffer3,
                            1);  //开启下一次接收中断
    }
}

/**
* @brief Write data to the ring buffer
* @param [in] pRb        :ringbuffer adress
* @param [in] buf        : buf adress
* @param [in] len        : byte length
* @return   correct : Returns the length of the written data
            failure : -1
*/
int32_t gizPutData(rb_t* pRb, uint8_t* buf, uint32_t len)
{
    int32_t count = 0;

    if (NULL == buf)
    {
        printf("ERR: gizPutData buf is empty \n");
        return -1;
    }

    count = rbWrite(pRb, buf, len);
    if (count != len)
    {
        printf("ERR: Failed to rbWrite \n");
        return -1;
    }

    return count;
}

/**
* @brief gizwits Protocol initialization interface
* Protocol-related timer, serial port initialization
* Datapoint initialization
* @param none
* @return none
* 初始化环形缓冲区
rb1--PM25 rb2--NULL rb3--Esp12F
*/
void ringBufferInit(void)
{
    rb1.rbCapacity = RB_PM_MAX_LAN;
    rb1.rbBuff = rbBuf1;
    if (0 == rbCreate(&rb1))
    {
        printf("rbCreate  rb1 Success \n");
    }
    else
    {
        printf("rbCreate rb1 Faild \n");
    }
    // memset((uint8_t *)&gizwitsProtocol, 0, sizeof(gizwitsProtocol_t));

    rb2.rbCapacity = RB_MAX_LEN;
    rb2.rbBuff = rbBuf2;
    if (0 == rbCreate(&rb2))
    {
        printf("rbCreate  rb2 Success \n");
    }
    else
    {
        printf("rbCreate rb2 Faild \n");
    }
    // memset((uint8_t *)&gizwitsProtocol, 0, sizeof(gizwitsProtocol_t));

    rb3.rbCapacity = RB_MAX_LEN;
    rb3.rbBuff = rbBuf3;
    if (0 == rbCreate(&rb3))
    {
        printf("rbCreate  rb3 Success \n");
    }
    else
    {
        printf("rbCreate rb3 Faild \n");
    }
    // memset((uint8_t *)&gizwitsProtocol, 0, sizeof(gizwitsProtocol_t));
}
